package com.dq.controller.serviceproject;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dq.annotation.aop.SysLog;
import com.dq.exception.customException.AdminException;
import com.dq.controller.in.ServiceProjectQuery;
import com.dq.controller.in.base.BaseAdminQuery;
import com.dq.vo.ListDataVo;
import com.dq.vo.Vo;
import com.dq.vo.baseVo.VO;
import com.dq.domain.base.impl.BaseDaoImpl;
import com.dq.domain.service.ServiceProject;
import com.dq.domain.service.centre.ServiceProjectCategoryCentre;
import com.dq.service.AllService;
import com.dq.utils.BeanNullUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author cf
 */
@Api(description = "服务项目管理")
@RestController
@RequestMapping("sys/serviceProject/")
public class ServiceProjectController extends AllService {

    @ApiOperation("所有服务项目列表")
    @GetMapping("listAll")
    public Vo listAll()throws AdminException{
        List<ServiceProject> serviceProjects = serviceProjectRepository.findByDeletedFalse();

        JSONArray array = new JSONArray();
        serviceProjects.forEach(serviceProject -> {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("id",serviceProject.getId());
            jsonObject.put("name",serviceProject.getProjectName());
            jsonObject.put("data", serviceProject.toVO());
            array.add(jsonObject);
        });
        return new ListDataVo(array);
    }

    @ApiOperation("获取所有 服务项目")
    @PostMapping("/list")
    @RequiresPermissions("sys:serviceProject:list")
    public Vo list(@ApiParam("") @RequestBody ServiceProjectQuery serviceProjectQuery) throws AdminException {
        serviceProjectQuery.format();

        List<ServiceProject> list = serviceProjectService.list(serviceProjectQuery);

        list.forEach(node -> {
            List<ServiceProjectCategoryCentre> ls = serviceProjectCategoryCentreRepository.findByDeletedAndServiceProjectOrderByIdDesc(false, node);
            List<Long> serviceCategoryIds = new ArrayList<>();
            List<String> serviceCategoryNames = new ArrayList<>();
            ls.forEach(n -> {
                serviceCategoryIds.add(n.getServiceProjectCategory().getId());
                serviceCategoryNames.add(n.getServiceProjectCategory().getCategoryName());
            });
            node.setServiceCategoryIds(serviceCategoryIds);
            node.setServiceCategoryNames(serviceCategoryNames);
        });

        return new ListDataVo((List<ServiceProject.Vo>) VO.toVOCollection(list), serviceProjectService.count(serviceProjectQuery));
    }

    @ApiOperation("添加")
    @PostMapping("/save")
    @RequiresPermissions("sys:serviceProject:save")
    public Vo list(@RequestBody ServiceProject serviceProject) throws AdminException {

        List<ServiceProject> nameList = serviceProjectRepository.findByDeletedAndProjectName(false, serviceProject.getProjectName());
        if(nameList != null && nameList.size() > 0) {
            throw new AdminException("此项目已存在");
        }
        if(serviceProject.getServiceCategoryIds().size() <= 0) {
            throw new AdminException("请选择类别");
        }

        ServiceProject save = serviceProjectService.save(serviceProject);

        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("修改")
    @PostMapping("/update")
    @RequiresPermissions("sys:serviceProject:update")
    public Vo update(@RequestBody ServiceProject serviceProject) throws AdminException {
        if(serviceProject.getId() == null) {
            throw new AdminException("缺参id");
        }
        ServiceProject serviceProjectQuery = dao.get(ServiceProject.class, serviceProject.getId());
        if (serviceProjectQuery == null) {
            throw new AdminException("无对应id对象");
        }
        if(StringUtils.isNotBlank(serviceProject.getProjectName())) {
            if(!serviceProjectQuery.getProjectName().equals(serviceProject.getProjectName())) {
                List<ServiceProject> nameList = serviceProjectRepository.findByDeletedAndProjectName(false, serviceProject.getProjectName());
                if(nameList != null && nameList.size() > 0) {
                    throw new AdminException("此项目已存在");
                }
            }
        }

        BeanUtils.copyProperties(serviceProject, serviceProjectQuery, BeanNullUtil.getNullPropertyNames(serviceProject));
        serviceProjectService.update(serviceProjectQuery);
        return new Vo(HttpStatus.OK);
    }

    @ApiOperation("删除")
    @SysLog("删除")
    @PostMapping("/delete/{id}")
    @RequiresPermissions("sys:serviceProject:delete")
    public Vo delete(@PathVariable @ApiParam(value = "id") Long id) throws AdminException {
        ServiceProject serviceProject = dao.get(ServiceProject.class, id);
        if (serviceProject == null) {
            throw new AdminException("无对应id对象");
        }
        serviceProject.setDeleted(true);
        serviceProjectService.update(serviceProject);
        return new Vo(HttpStatus.OK);
    }

}
